package com.example;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by zhangguobao on 16/10/20.
 */
public class IOTest {


    public static void main(String[] args) {
        method3();
    }

    public static void method3(){
        try {
            ByteArrayOutputStream bOutPut = new ByteArrayOutputStream();
//            while (bOutPut.size() != 12) {
                bOutPut.write("asdasda".getBytes());
//            }
            byte b[] = bOutPut.toByteArray();
            for (byte b1 : b) {
                System.out.println(b1);
            }

            ByteArrayInputStream bInPut = new ByteArrayInputStream(b);
            int temp = 0;
            while ((temp = bInPut.read()) != -1) {
                System.out.println(Character.toUpperCase((char) temp));
            }
            bInPut.reset();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void method2(){
        try {
            File file = new File("/Users/zhangguobao/AndroidProject/AppTest/javatest/src/main/java/com/example/testfile");
            InputStream in  = new FileInputStream(file);
            byte[] b = new byte[1024];
            int count = 0;
            int temp = 0;
            while ((temp = in.read(b)) != -1) {
                break;
            }


            System.out.println(new String(b,0,temp));
//            System.out.println(b);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void method1(){
        FileReader reader = null;

        try {
            reader = new FileReader("/Users/zhangguobao/Desktop/test/test.txt");
            char[] chars = new char[1024];
            int num = 0;
            while ((num = reader.read(chars)) != -1) {
                System.out.println("sds  = " + new String(chars, 0, num));
            }
        } catch (IOException e) {
            try {
                if (reader != null)
                    reader.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
    }
}
